/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.client.ZMessage.ZMimePart;
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.zmime.ZMimeMessage;
/*     */ import com.zimbra.cs.mailclient.smtp.SmtpConfig;
/*     */ import com.zimbra.cs.mailclient.smtp.SmtpConnection;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import javax.mail.Message.RecipientType;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestSmtpClient
/*     */   extends TestCase
/*     */ {
/*     */   private static final String USER_NAME = "user1";
/*     */   private static final String USER2_NAME = "user2";
/*  40 */   private static final String NAME_PREFIX = TestSmtpClient.class.getSimpleName();
/*     */   private final String mHost;
/*     */   private final int mPort;
/*     */   
/*     */   public TestSmtpClient() throws Exception
/*     */   {
/*  46 */     this.mHost = TestUtil.getServerAttr("zimbraSmtpHostname");
/*  47 */     this.mPort = Integer.parseInt(TestUtil.getServerAttr("zimbraSmtpPort"));
/*     */   }
/*     */   
/*     */   public void setUp() throws Exception
/*     */   {
/*  52 */     cleanUp();
/*     */   }
/*     */   
/*     */   public void testSimple() throws Exception {
/*  56 */     String sender = "user1";
/*  57 */     String[] recipients = { "user1" };
/*  58 */     sendAndVerify(sender, recipients, "1 line", "1 line", "1 line\r\n");
/*  59 */     sendAndVerify(sender, recipients, "1 line crlf", "1 line crlf\r\n", "1 line crlf");
/*  60 */     sendAndVerify(sender, recipients, "2 lines", "line1\r\nline2", "line1\r\nline2");
/*  61 */     sendAndVerify(sender, recipients, "2 lines crlf", "line1\r\nline2\r\n", "line1\r\nline2");
/*     */   }
/*     */   
/*     */   public void testTwoRecipients() throws Exception {
/*  65 */     String sender = "user1";
/*  66 */     String[] recipients = { "user1", "user2" };
/*  67 */     sendAndVerify(sender, recipients, "2 recipients", "2 recipients\r\n", "2 recipients\r\n");
/*     */   }
/*     */   
/*     */   public void testTransparency() throws Exception {
/*  71 */     String sender = "user1";
/*  72 */     String[] recipients = { "user1" };
/*  73 */     sendAndVerify(sender, recipients, "transparency1", "..line1\r\n", "..line1\r\n");
/*  74 */     sendAndVerify(sender, recipients, "transparency2", "line1\r\n.line2\r\n..line3\r\n...line4\r\n", "line1\r\n.line2\r\n..line3\r\n...line4\r\n");
/*     */   }
/*     */   
/*     */   public void testMimeMessage() throws Exception
/*     */   {
/*  79 */     MimeMessage mm = new ZMimeMessage(JMSession.getSession());
/*  80 */     InternetAddress addr = new JavaMailInternetAddress(TestUtil.getAddress("user1"));
/*  81 */     mm.setFrom(addr);
/*  82 */     mm.setRecipient(Message.RecipientType.TO, addr);
/*  83 */     String subject = NAME_PREFIX + " testMimeMessage";
/*  84 */     mm.setSubject(subject);
/*  85 */     mm.setText("testMimeMessage");
/*  86 */     mm.saveChanges();
/*     */     
/*     */ 
/*  89 */     SmtpConfig config = new SmtpConfig(this.mHost, this.mPort, "localhost");
/*  90 */     SmtpConnection conn = new SmtpConnection(config);
/*  91 */     conn.sendMessage(mm);
/*     */     
/*     */ 
/*  94 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*  95 */     TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/*     */     
/*     */ 
/*  98 */     conn.sendMessage(addr.getAddress(), new String[] { TestUtil.getAddress("user2") }, mm);
/*  99 */     mbox = TestUtil.getZMailbox("user2");
/* 100 */     TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/*     */   }
/*     */   
/*     */   private void sendAndVerify(String sender, String[] recipients, String subject, String body, String expectedBody) throws Exception
/*     */   {
/* 105 */     if (sender.indexOf("@") < 0) {
/* 106 */       sender = TestUtil.getAddress(sender);
/*     */     }
/* 108 */     for (int i = 0; i < recipients.length; i++) {
/* 109 */       if (recipients[i].indexOf("@") < 0) {
/* 110 */         recipients[i] = TestUtil.getAddress(recipients[i]);
/*     */       }
/*     */     }
/* 113 */     if (subject.indexOf(NAME_PREFIX) < 0) {
/* 114 */       subject = NAME_PREFIX + " " + subject;
/*     */     }
/*     */     
/*     */ 
/* 118 */     String content = new MessageBuilder().withFrom(sender).withToRecipient(recipients[0]).withSubject(subject).withBody(body).create();
/* 119 */     SmtpConfig config = new SmtpConfig(this.mHost, this.mPort, "localhost");
/* 120 */     SmtpConnection smtp = new SmtpConnection(config);
/* 121 */     smtp.sendMessage(sender, recipients, content);
/*     */     
/*     */ 
/* 124 */     for (String recipient : recipients) {
/* 125 */       ZMailbox mbox = TestUtil.getZMailbox(recipient);
/* 126 */       ZMessage msg = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/* 127 */       String currentBody = getBodyContent(msg.getMimeStructure());
/* 128 */       TestUtil.assertMessageContains(currentBody, expectedBody);
/*     */     }
/*     */   }
/*     */   
/*     */   private String getBodyContent(ZMessage.ZMimePart part) {
/* 133 */     if (part.isBody()) {
/* 134 */       return part.getContent();
/*     */     }
/* 136 */     for (ZMessage.ZMimePart child : part.getChildren()) {
/* 137 */       String content = getBodyContent(child);
/* 138 */       if (content != null) {
/* 139 */         return content;
/*     */       }
/*     */     }
/* 142 */     return null;
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 147 */     cleanUp();
/*     */   }
/*     */   
/*     */   public void cleanUp() throws Exception {
/* 151 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/* 152 */     TestUtil.deleteTestData("user2", NAME_PREFIX);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 156 */     TestUtil.cliSetup();
/* 157 */     TestUtil.runTest(TestSmtpClient.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestSmtpClient.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */